package com.ljw.easyrulestest1;

import org.jeasy.rules.api.*;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.core.RuleBuilder;
import org.junit.Test;
import org.mvel2.MVEL;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.integration.impl.MapVariableResolverFactory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lijunwei
 * @date 2024/2/4
 */
public class DefaultRulesEngineTest {

    public static void main(String[] args) {
        RulesEngineParameters parameters = new RulesEngineParameters()
                .priorityThreshold(10)//大于rulePriorityThreshold时跳过下一个规则
                .skipOnFirstAppliedRule(true)//在应用规则时跳过下一个规则
                .skipOnFirstFailedRule(true)//在规则失败时跳过下一个规则
                .skipOnFirstNonTriggeredRule(true);//在未触发规则时跳过下一个规则

        RulesEngine rulesEngine = new DefaultRulesEngine(parameters);
    }

    @Test
    public void test01() {
        Rules rules = new Rules();

        Rule weatherRule = new RuleBuilder()
                .name("weather rule")
                .description("if it rains then take an umbrella")
                .when(facts -> facts.get("rain").equals(true))
                .then(facts -> System.out.println("It rains, take an umbrella!"))
                .build();

        rules.register(weatherRule);

        Rule weatherRule1 = new RuleBuilder()
                .name("weather rule1")
                .description("if it rains then take an umbrella1")
                .when(facts -> facts.get("rain").equals(false))
                .then(facts -> System.out.println("It rains, take an umbrella!111111111111111"))
                .build();

        rules.register(weatherRule1);

        // define facts
        Facts facts = new Facts();
        facts.put("rain", true);

        // fire rules on known facts
        RulesEngineParameters parameters = new RulesEngineParameters()
                .priorityThreshold(1)//大于rulePriorityThreshold时跳过下一个规则
                .skipOnFirstAppliedRule(true)//在应用规则时跳过下一个规则
                .skipOnFirstFailedRule(true)//在规则失败时跳过下一个规则
                .skipOnFirstNonTriggeredRule(true);//在未触发规则时跳过下一个规则
       // RulesEngine rulesEngine = new InferenceRulesEngine(parameters);
        RulesEngine rulesEngine = new DefaultRulesEngine(parameters);
        rulesEngine.fire(rules, facts);

        System.out.println(rules.size());

    }

    /**
     * https://vimsky.com/examples/detail/java-method-org.mvel2.MVEL.executeExpression.html
     *
     * @param args
     */
    @Test
    public void test02() {
        Foo foo = new Foo();
        foo.setName("test");

        Map context = new HashMap();
        context.put("foo", foo);
        VariableResolverFactory functionFactory = new MapVariableResolverFactory(context);

        String expression = "foo.name == 'test'";

        Boolean result = (Boolean) MVEL.eval(expression, functionFactory);
        System.out.println(result);


        Serializable compileExpression = MVEL.compileExpression(expression);
        result = (Boolean) MVEL.executeExpression(compileExpression, context, functionFactory);
        System.out.print(result);

    }

    @Test
    public void executeExpression() {

        Map contextMap = new HashMap();
        contextMap.put("reformResult", 1);
        contextMap.put("ruleCreateShop", 1);
        contextMap.put("sameSeasonTechPunishmentNum", 0);

        contextMap.put("totalData", 1);

        VariableResolverFactory functionFactory = new MapVariableResolverFactory(contextMap);

        String expression = "reformResult==0 &&ruleCreateShop==1 &&sameSeasonTechPunishmentNum<21 && (totalData==null || totalData==1)";

        Boolean result = (Boolean) MVEL.eval(expression, functionFactory);
        System.out.println(result);


        Serializable compileExpression = MVEL.compileExpression(expression);
        result = (Boolean) MVEL.executeExpression(compileExpression, new HashMap(), functionFactory);
        System.out.print(result);
    }

}
